package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatementItemServiceImpl implements IStatementItemService {

    @Autowired
    private StatementItemMapper statementItemMapper;

    @Autowired
    private IStatementService statementService;


    @Override
    public TablePageInfo<StatementItem> query(StatementItemQuery qo) {

        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<StatementItem>(statementItemMapper.selectForList(qo));
    }


    // 删除
    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    @Transactional
    public void saveItems(List<StatementItem> items) {
        // 删除最后的元素并保存
        StatementItem tempItem = items.remove(items.size() - 1);
        // 优惠价格
        BigDecimal itemPrice = tempItem.getItemPrice();
        // 获取statementId
        Long statementId = tempItem.getStatementId();
        // 根据id 获取Statement(结算单) 对象
        Statement statement = statementService.get(statementId);
        // 合理校验
        // 当结算单的状态为已支付
        if (Statement.STATUS_PAID.equals(statement.getStatus())){
            throw new BusinessException("已支付的结算单不能在进行保存");
        }
        // 当结算单的状态为消费中
        statementItemMapper.deleteByStatementId(statementId);
        // new 服务项总金额和总数量
        BigDecimal totalAmount = new BigDecimal("0.00");
        BigDecimal totalQuantity = new BigDecimal("0");
        for (StatementItem item : items){
            statementItemMapper.insert(item);
            // 总金额 = (服务项价格 * 服务项数量) 累加
            totalAmount = totalAmount.add(item.getItemPrice().multiply(item.getItemQuantity()));
            // 总数量 = 累加每项服务的数量
            totalQuantity = totalQuantity.add(item.getItemQuantity());
        }

        if (itemPrice.compareTo(totalAmount)>0){
            throw new BusinessException("非法操作,折扣金额不能大于总金额");
        }

        statementService.updateAmount(statement.getId(),totalAmount,totalQuantity,itemPrice);
    }

    @Override
    public void deleteByStatementId(Long statementId) {
        // 根据
        statementItemMapper.deleteByStatementId(statementId);
    }
}
